package scau.osdesign.model;

public class Philosopher extends Thread{
    private int id;
    private PhilosopherStates status;
    public boolean hasLeftFork=false;
    public boolean hasRightFork=false;
    private int leftForkId;
    private int rightForkId;
    public boolean isPaused;

    public Philosopher(int id){this.id = id;}

    public void setId(int id ){
        this.id = id;
    }
    public int getPhiId(){return this.id;}

    public void getLeftFork(int id){
        synchronized (this){
            this.hasLeftFork = true;
            this.leftForkId = id;
        }
    }

    public void getRightFork(int id){
        synchronized (this){
            this.hasRightFork = true;
            this.rightForkId = id;
        }
    }

    public void releaseLeftFork(){
        synchronized (this){
            this.hasLeftFork = false;
        }
    }

    public void releaseRightFork(){
        synchronized (this){
            this.hasRightFork = false;
        }
    }

    public PhilosopherStates getStates(){
        return this.status;
    }

    public void waiting(){
        synchronized (this){
            try{
                this.status = PhilosopherStates.WAITING;
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        //"我是第"+id+"号哲学家,我在等待叉子"
    }

    public void eating(){
        synchronized (this){
            try {
                this.status = PhilosopherStates.EATING;
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        //"我是第"+id+"号哲学家,我得到了左右的叉子，正在就餐";
    }

    public void thinking(){
        synchronized (this){
            try{
                this.status = PhilosopherStates.THINKING;
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        //"我是第"+id+"号哲学家,我正在思考";
    }

    public void waitSelf(){
        synchronized (this){
            try{
                isPaused = true;
                System.out.println("线程被阻塞了");
                this.wait();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    public void awakeSelf(){
        synchronized (this){
            isPaused = false;
            System.out.println("线程被唤醒了");
            this.notify();
        }
    }

    public void interruptSelf(){
        synchronized (this){
            this.interrupt();
        }
    }

    public void finish(){
        //"我是第"+id+"号哲学家,我吃完了，正在放下叉子";
    }

    public boolean isHungry(){
        return false;
    }

}
